/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thesis.ramstorage.hash;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 *
 * @author Admin
 * Use 20bit (m = 20)
 */
public class HashFunction {

    private int keySize = 15;

    public HashFunction(int keySize) {
        this.keySize = keySize;
    }

    public int hash(String id) {
        return (int) (Math.abs(id.hashCode()) % Math.pow(2, keySize));
    }

    public BigInteger generateSHA1(String key) {

        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            byte[] hashMsg = md.digest(key.toString().getBytes());
            BigInteger bi = new BigInteger(hashMsg).abs();
            BigInteger mod = new BigInteger(Integer.toString((int) Math.pow(2, keySize)));
            return bi.mod(mod);
        } catch (Exception e) {
        }
        return null;
    }
}
